package com.dsj.gxrd.modules.book.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsj.gxrd.modules.book.domain.dto.RdBookDto;
import com.dsj.gxrd.modules.book.domain.dto.RdFileDto;
import com.dsj.gxrd.modules.book.domain.entity.RdBook;
import com.dsj.gxrd.modules.book.domain.query.RdBookQuery;
import com.dsj.gxrd.modules.book.enums.FileRelationTypeEnum;
import com.dsj.gxrd.modules.book.mapper.RdBookMapper;
import com.dsj.gxrd.modules.book.service.IRdBookService;
import com.dsj.gxrd.modules.book.service.IRdFileService;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-11
 */
@Service
public class RdBookServiceImpl extends ServiceImpl<RdBookMapper, RdBook> implements IRdBookService {

    @Autowired
    private IRdFileService rdFileService;


    @Override
    public RdBookDto selectRdBookByRdBookId(Long rdBookId) {
        List<RdFileDto> list = rdFileService.listFiles(rdBookId, FileRelationTypeEnum.BOOK_SWEET_COMM_SHARE_IMAGE);
        RdBook rdBook = this.baseMapper.selectById(rdBookId);
        RdBookDto rdBookDto = BeanUtils.copyProperties(rdBook, RdBookDto.class);
        rdBookDto.setFileList(list);
        return rdBookDto;
    }

    @Override
    public List<RdBookDto> selectRdBookList(RdBookQuery rdBook) {
        Long sysBookCatId = rdBook.getSysBookCatId();
        String bookName = rdBook.getBookName();
        String referrer = rdBook.getReferrer();
        Integer approvalStatus = rdBook.getApprovalStatus();
        List<RdBook> list = this.lambdaQuery()
                .like(StringUtils.isNotEmpty(bookName), RdBook::getBookName, bookName)
                .like(StringUtils.isNotEmpty(referrer), RdBook::getReferrer, referrer)
                .eq(sysBookCatId != null, RdBook::getSysBookCatId, sysBookCatId)
                .eq(approvalStatus != null, RdBook::getApprovalStatus, approvalStatus)
                .orderByAsc(RdBook::getBookSort)
                .orderByDesc(RdBook::getCreateTime)
                .list();
        List<RdBookDto> rdBookList = BeanUtils.copyProperties(list, RdBookDto.class);
        rdBookList.forEach(item -> {
            List<RdFileDto> fileDtoList =
                    rdFileService.listFiles(item.getRdBookId(), FileRelationTypeEnum.BOOK_SWEET_RECOMMEND_BOOKS_IMAGE);
            item.setFileList(fileDtoList);
        });
        return rdBookList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertRdBook(RdBookDto rdBookDto) {
        //新增书籍数据到数据库
        List<RdFileDto> fileList = rdBookDto.getFileList();
        RdBook rdBook = BeanUtils.copyProperties(rdBookDto, RdBook.class);
        rdBook.setDelFlag(0);
        rdBook.setApprovalStatus(0);
        boolean i = save(rdBook);
        rdFileService.saveFileBatch(fileList, rdBook.getRdBookId(), FileRelationTypeEnum.BOOK_SWEET_RECOMMEND_BOOKS_IMAGE);
        return i ? 1 : 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateRdBook(RdBookDto rdBookDto) {
        //修改文件集
        rdFileService.updateFileBatch(rdBookDto.getFileList(), rdBookDto.getRdBookId(), FileRelationTypeEnum.BOOK_SWEET_RECOMMEND_BOOKS_IMAGE);
        RdBook rdBook = BeanUtils.copyProperties(rdBookDto, RdBook.class);
        return updateById(rdBook) ? 1 : 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteRdBookByRdBookIds(List<Long> rdBookIds) {
        //删除对应的文件
        for (Long rdBookId : rdBookIds) {
            rdFileService.deleteFilesByRelationId(rdBookId, FileRelationTypeEnum.BOOK_SWEET_RECOMMEND_BOOKS_IMAGE);
        }
        return removeByIds(rdBookIds) ? 1 : 0;
    }

    @Override
    public int deleteRdBookByRdBookId(Long rdBookId) {
        return 0;
    }

    @Override
    public boolean approval(RdBookDto rdBook) {
        return false;
    }

}
